package linear;

/**
 * 顺序表
 * @author huangkh
 */
public class Sequence<E> implements Linear {

    /**
     *
     */
    private static final Object[] EMPTY_ELEMDATA = {};

    private static final int DEFAULT_CAPACITY = 10;

    private static final float LOAD_FACT = 0.75f;

    /**
     * 当前容量
     */
    private int capacity;

    /**
     * 元素数量
     */
    private int size;

    Object[] elemData;

    public Sequence(int initialCapacity) {
        if (initialCapacity>0){
            elemData = new Object[initialCapacity];
            capacity = initialCapacity;
        }else if (initialCapacity==0){
            elemData = EMPTY_ELEMDATA;
            capacity = 0;
        }else {
            throw new IllegalArgumentException("非法参数：" + initialCapacity) ;
        }
    }

    public Sequence(){
        elemData = new Object[DEFAULT_CAPACITY];
        capacity = DEFAULT_CAPACITY;
    }

    @Override
    public void clear() {
        for (int i = 0; i < size; i++) {
            elemData[i] = null;
        }
        size = 0;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void each() {
        System.out.print("size:" + size );
        for (int i = 0; i < size; i++) {
            System.out.print( ",\t" + elemData[i].toString());
        }
    }

    @Override
    public Object elemAt(int index) {
        if (index<0 || index> size){
            throw new IllegalArgumentException("非法索引值：" + index);
        }
        return elemData[index];
    }

    @Override
    public int indexOf(Object o) {
        if (o==null){
            for (int i=0;i<size;i++){
                if (elemData[i]==null){
                    return i;
                }
            }
        }else{
            for (int i=0;i<size;i++){
                if (o.equals(elemData[i])){
                    return i;
                }
            }
        }
        return -1;
    }

    @Override
    public int insertElem(Object e, int index) {
        if (index<0 || index>size){
            throw new IllegalArgumentException("非法索引值：" + index);
        }
        if (LOAD_FACT * capacity <= size){
            grow();
        }
        Object temp = null;
        for (int i = index; i < size + 1; i++) {
            temp = elemData[i];
            elemData[i] = e;
            e = temp;
        }
        size = size + 1;
        return size;
    }

    private void grow() {
        System.out.println("当前capacity:" + capacity + ",当前size: " + size);
        if (capacity==0){
            capacity = 10;
        }else{
            capacity = capacity * 2;
        }
        System.out.println("扩容后的capacity:" + capacity+ ",当前size: " + size);
        Object[] objectsNew = new Object[capacity];
        for (int i = 0; i < size; i++) {
            objectsNew[i] = elemData[i];
        }
        elemData = objectsNew;
    }

    @Override
    public int deleteElem(int index) {
        if (index<0 || index>size){
            throw new IllegalArgumentException("非法索引值：" + index);
        }
        Object temp = null;
        for (int i = index; i < size-1; i++) {
            temp = elemData[i+1];
            elemData[i] = temp;
        }
        elemData[size-1] = null;
        size = size -1;
        return size;
    }

    @Override
    public boolean add(Object o) {
        if (LOAD_FACT * capacity <= size){
            grow();
        }
        elemData[size] = o;
        size = size + 1;
        return false;
    }

    public int getCapacity(){
        return this.capacity;
    }

}
